//
//  ELUtils.m
//  ELBooks
//
//  Created by Eric on 12-8-25.
//  Copyright 2012 zhaopin. All rights reserved.
//

#import "ELUtils.h"
#import "CustomAlertView.h"
#import <sys/sysctl.h>
#import "BaseViewController.h"
#import "sys/utsname.h"

NSUInteger DeviceSystemMajorVersion() {
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion]
                                       componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
    });
    
    return _deviceSystemMajorVersion;
}
NSString*  DeviceDetailInfo() {
    static NSString * _deviceDetailInfo = @"";
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString * systemVersion = [[UIDevice currentDevice] systemVersion];
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        NSString * os = @"iOS";
        NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:deviceString,@"model",os,@"os",systemVersion,@"version", nil];
        
       _deviceDetailInfo = [dict JSONString];
        
    });
    return _deviceDetailInfo;
}
void showAlertMessage(NSString *message)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:message message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}


void showCustomAlertMessage(NSString *message)
{
    if (message==nil || [message length]<1) {
        return;
    }
    CustomAlertView *customAlartView = (CustomAlertView *)[theApp.window viewWithTag:980153];
    if (customAlartView) {
        [customAlartView removeFromSuperview];
        customAlartView = nil;
    }
//    161 78
    customAlartView = [[CustomAlertView alloc] initWithFrame:CGRectMake(80, 460 / 2, 160, 50)] ;
    customAlartView.titleLabel.text = message;
    customAlartView.tag = 980153;
    
    [theApp.window addSubview: customAlartView];
}

void showFullScreen(BOOL flag)
{
    theApp.window.userInteractionEnabled = !flag;
}
void showFullView(BOOL flag,BaseViewController * bvc)
{
    bvc.view.userInteractionEnabled = !flag;
}
void showIndicator(BOOL flag,NSString * title)
{
    UIView *activityView = (UIView *)[theApp.window viewWithTag:70021];
    if (flag) {
        if (activityView==nil) {
            activityView = [[UIView alloc] initWithFrame:CGRectMake(110, 120, 100, 100)];
            activityView.tag = 70021;
            activityView.backgroundColor = [UIColor clearColor];
            UIView * v = [[UIView alloc]initWithFrame:activityView.bounds];
            v.backgroundColor = [UIColor blackColor];
            v.alpha = .7;
            v.layer.cornerRadius = 5;
            v.layer.masksToBounds = YES;
            [activityView addSubview:v];
            [activityView.layer setContents:(id)[[UIImage imageNamed:@"incativy.png"] CGImage]];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3, 65, 95, 32)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = title;
            label.numberOfLines = 0;
            label.font = [UIFont CustomFontGBKSize:12];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            [activityView addSubview:label];
            

            
            
            UIImageView * imgv = [[UIImageView alloc]init];
            imgv.animationImages = @[IMG(@"01.png"),
                                     IMG(@"02.png"),
                                     IMG(@"03.png"),
                                     IMG(@"04.png"),
                                     IMG(@"05.png"),
                                     IMG(@"06.png"),
                                     IMG(@"07.png"),
                                     IMG(@"08.png")];
            imgv.frame = CGRectMake(40, 30, 20, 20);
            imgv.animationDuration = 1;
            imgv.animationRepeatCount = 0;
            [activityView addSubview:imgv];
            imgv.tag = 794232;
            [imgv startAnimating];
            activityView.center = theApp.window.center;
            [theApp.window addSubview:activityView];

        }
        
    }
    else{
        if (activityView && [activityView superview]) {
            UIImageView *activityIndicator = (UIImageView *)[activityView viewWithTag:794232];
            if (activityIndicator) {
                [activityIndicator stopAnimating];
            }
            [activityView removeFromSuperview];
        }
    }
    
}

void showLoadView(BOOL flag)
{
    if (flag) {
        UIView *loadView = [[UIView alloc] init];
        loadView.frame = CGRectMake(0, 0, 320, ScreenHeight);
        loadView.alpha = 0.5;
        loadView.center = theApp.window.center;
        loadView.tag = 794232;
        loadView.backgroundColor = [UIColor blackColor];
        [theApp.window addSubview:loadView];
        
        UIImageView * imgv = [[UIImageView alloc]init];
        imgv.animationImages = @[IMG(@"loading00.png"),
                                 IMG(@"loading01.png"),
                                 IMG(@"loading02.png"),
                                 IMG(@"loading03.png"),
                                 IMG(@"loading04.png"),
                                 IMG(@"loading05.png")];
        imgv.frame = CGRectMake(40, 30, 100, 100);
        imgv.animationDuration = 1;
        imgv.animationRepeatCount = 0;
        
        imgv.tag = 794233;
        [imgv startAnimating];
        imgv.center = theApp.window.center;
        [theApp.window addSubview:imgv];
    }
    else{
        UIImageView *imgt = (UIImageView *)[theApp.window viewWithTag:794233];
        if (imgt && [imgt superview]) {
            [imgt removeFromSuperview];
        }
        UIView *loadView = [theApp.window viewWithTag:794232];
        if (loadView && [loadView superview]) {
            [loadView removeFromSuperview];
        }
    }
}


//限制字数
float getTextLength(NSString *text)
{
    
    float num = 0;
    for (int i =0; i<[text length]; i++) {
        
        NSRange  range = {i,1};
        
        NSString  *str = [text substringWithRange:range];
        
        if (![str isEqualToString:@""] || str != nil)
        {
            int  charLenth = (int)[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
            if (charLenth == 3) {
                num += 1;
            }
            if (charLenth == 1) {
                num += 0.5;
            }
        }
        else
        {
            num += 0.5;
        }
    }
    return num;
}


/*
 函数名:   IsValidEmail
 作者:     eric
 创建日期:  2012年8月10日
 描述:     用于判断主流邮箱的校验.若返回nil合法邮箱，否则不合法。
 */
NSString* IsValidEmail(NSString *pszEmail)
{
    if(pszEmail == nil || [pszEmail length]<1)
    {
        return @"请输入邮箱";
    }
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:pszEmail]?nil:@"邮箱不合法";
    
}

/*
 函数名:   IsValidMobeilTel
 作者:     eric
 创建日期:  2012年8月10日
 描述:     用于判断目前中华人民共和国三大运营商的手机号码合法性校验，若返回0，1，2表示合法，否则不合法
 */
NSString* IsValidMobeilTel(const char* pszTel)
{
    NSString* tyle = @"-1";
    int len = (int)strlen(pszTel);
    
    if(pszTel == NULL || len<1)
    {
        return @"请填写手机号";
    }
    
    
    if(len != 11)
    {
        return @"手机号码的长度不合要求";
    }
    int i = 0;
    while (i < len)
    {
        if(!isdigit(pszTel[i]))
        {
            return @"手机号码中包含不合法的数字";
        }
        
        if(i == 0)
        {
            if(pszTel[i] != '1')
            {
                return @"手机号码格式不合要求";
            }
        }
        else if(i == 1)
        {
            if(pszTel[i] != '3' && pszTel[i] != '4' && pszTel[i] != '5' && pszTel[i] != '8')
            {
                return @"手机号码格式不合要求";
            }
        }
        else if(i == 2)
        {
            if(( pszTel[i-1] == '3' && (pszTel[i] == '0' || pszTel[i] == '1' || pszTel[i] == '2')) ||
               (pszTel[i-1] == '4' && pszTel[i] == '5') ||
               (pszTel[i-1] == '5' && (pszTel[i] == '5' || pszTel[i] == '6'))||
               (pszTel[i-1] == '8' && (pszTel[i] == '5' || pszTel[i] == '6')))
            {
                tyle = @"0";
            }
            else if((pszTel[i-1] == '3' && (pszTel[i] == '4' || pszTel[i] == '5' || pszTel[i] == '6' || pszTel[i] == '7' || pszTel[i] == '8' || pszTel[i] == '9')) ||
                    (pszTel[i-1] == '4' && pszTel[i] == '7') ||
                    (pszTel[i-1] == '5' && (pszTel[i] == '0' || pszTel[i] == '1' || pszTel[i] == '2' || pszTel[i] == '7' || pszTel[i] == '8' || pszTel[i] == '9'))||
                    (pszTel[i-1] == '8' && (pszTel[i] == '2' || pszTel[i] == '3' || pszTel[i] == '7' || pszTel[i] == '8')))
            {
                tyle = @"1";
            }
            else if((pszTel[i-1] == '3' && pszTel[i] == '3') ||
                    (pszTel[i-1] == '5' && pszTel[i] == '3') ||
                    (pszTel[i-1] == '8' && (pszTel[i] == '9' || pszTel[i] == '0')))
            {
                tyle = @"2";
            }
            else
            {
                return @"手机号码不存在该号段.";
            }
        }
        i++;
    }
    return tyle;
}

//身份证号
BOOL validateIdentityCard(NSString * identityCard)
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    
    return [identityCardPredicate evaluateWithObject:identityCard];
}

NSString* IsValidPWD(const char* passWord)
{
    if(passWord == NULL)
    {
        return @"请输入密码";
    }
    int i = 0;
    
    while(*(passWord + i) != '\0')
    {
        char ch = *(passWord + i);
        if(!isdigit(ch) && !isalnum(ch) && ch!='_')
        {
            return @"密码应为6-25位的字母、数字或下划线";
        }
        i++;
    }
    return nil;
}

NSMutableArray * addPhoneNumArr(NSString *text)
{
    //匹配电话号码
    NSString *regex_phonenum = @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+\\d{10}|\\d{7,}";
    NSArray *array_phonenum = [text componentsMatchedByRegex:regex_phonenum];
    NSMutableArray *phoneNumArr = [NSMutableArray arrayWithArray:array_phonenum];
    return phoneNumArr;
}

NSMutableArray * addEmailArr(NSString *text)
{
    //匹配Email地址
    NSString *regex_email = @"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*";
    NSArray *array_email = [text componentsMatchedByRegex:regex_email];
    NSMutableArray *emailArr = [NSMutableArray arrayWithArray:array_email];
    return emailArr;
}

BOOL bankCard(NSString *cardID){
    NSString *regex = @"[0-9]{16,19}";
    NSPredicate *passwordText = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [passwordText evaluateWithObject:cardID];
}

BOOL validateMobile(NSString * phoneNum)
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1\\d{10}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1\\d{10}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1\\d{10}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1\\d{10}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:phoneNum] == YES)
        || ([regextestcm evaluateWithObject:phoneNum] == YES)
        || ([regextestct evaluateWithObject:phoneNum] == YES)
        || ([regextestcu evaluateWithObject:phoneNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

NSString* replaceIdCardNum(NSString *idcard){
    
    if(idcard!=nil&&(idcard.length==18||idcard.length==15) ){
        if(idcard.length == 18){
            NSString  *pre3 = [idcard substringToIndex:3];
            NSString  *end4 = [idcard substringFromIndex:14];
           // NSLog(@"pre3 %@  and end4 %@",pre3,end4);
            NSString  *finalStr = [[pre3 stringByAppendingString:@"***********"] stringByAppendingString:end4];
           // NSLog(@"finalStr %@",finalStr);
            return finalStr;

        }else if(idcard.length == 15){
            NSString  *pre3 = [idcard substringToIndex:3];
            NSString  *end4 = [idcard substringFromIndex:11];
            //NSLog(@"pre3 %@  and end4 %@",pre3,end4);
            NSString  *finalStr = [[pre3 stringByAppendingString:@"********"] stringByAppendingString:end4];
            //NSLog(@"finalStr %@",finalStr);
            return finalStr;
        }
    }
    return @"";
}




NSString* replaceBankCardNum(NSString *idbank){
    if (idbank!=nil&&idbank.length>=16) {
        NSString  *pre3 = [idbank substringToIndex:4];
        NSString  *end4 = [idbank substringFromIndex:idbank.length-4];
        int i = idbank.length -8;
        NSString *temp = @"********************";
        NSString *mid = [temp substringToIndex:i];
        NSString  *finalStr = [[pre3 stringByAppendingString:mid] stringByAppendingString:end4];
        //NSLog(@"finalStr %@",finalStr);
        return finalStr;
    }
    return @"";
    
}
NSString* mydeviceUniqueIdentifier()
{
    NSString *uniqueIdentifier = nil;
    if (isIOS7) {
        uniqueIdentifier = [UIDevice uniqueAdvertisingIdentifier];
    }
    else{
        uniqueIdentifier = [UIDevice uniqueGlobalDeviceIdentifier];
    }
    
    return uniqueIdentifier;
}

void showLoadingView() {    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    UIView *loadingView = (UIView *)[window viewWithTag:919191];
    UIImageView *loadingImageView = (UIImageView *)[window viewWithTag:919192];
    if (!loadingView) {
        loadingView = [[UIView alloc] initWithFrame:window.bounds];
        loadingView.backgroundColor = [UIColor blackColor];
        loadingView.alpha = 0.6;
        loadingView.tag = 919191;
        
//        UIActivityIndicatorView  *act=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//        act.center = loadingView.center;
//        [loadingView addSubview:act];
//        [act startAnimating];
    }
    if (!loadingImageView) {
        loadingImageView = [[UIImageView alloc]init];
        loadingImageView.animationImages = @[IMG(@"loading_1.png"),
                                             IMG(@"loading_2.png"),
                                             IMG(@"loading_3.png"),
                                             IMG(@"loading_4.png"),
                                             IMG(@"loading_5.png"),
                                             IMG(@"loading_6.png"),
                                             IMG(@"loading_7.png")];
        loadingImageView.size = CGSizeMake(117, 104);
        loadingImageView.center = window.center;
        loadingImageView.animationDuration = 1;
        loadingImageView.animationRepeatCount = 0;
        loadingImageView.tag = 919192;
    }
    
    [window addSubview:loadingView];
    [window addSubview:loadingImageView];
    [loadingImageView startAnimating];
}

void hideLoadingView() {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    UIView *loadingView = (UIView *)[window viewWithTag:919191];
    UIImageView *loadingImageView = (UIImageView *)[window viewWithTag:919192];
    if (loadingView) {
        [loadingView removeFromSuperview];
    }
    if (loadingImageView) {
        [loadingImageView stopAnimating];
        [loadingImageView removeFromSuperview];
    }
}

